﻿#pragma once
#include "UserTypes.h"

namespace Reign
{namespace Core
{
	template<class T> class ListArray
	{
		private://Vars
		T* value;
		uint count;
		public://Vars
		T* Stack() const {return value;}
		uint Count() const {return count;}
		
		public://Constructors
		ListArray()
		{
			null();
		}
		~ListArray()
		{
			if(value) delete value;
			null();
		}
		private://Constructors
		void null()
		{
			value = 0;
			count = 0;
		}
		
		public://Functions
		void Add(T pValue)
		{
			T* buff = new T[count+1];
			for(int i = 0; i != count; ++i) buff[i] = value[i];
			buff[count] = pValue;
			
			delete value;
			value = buff;
			++count;
		}

		void AddBefore(T pValue, uint pIndex) {AddAt(pValue, pIndex);}
		void AddAfter(T pValue, uint pIndex) {AddAt(pValue, pIndex+1);}
		void AddAt(T pValue, uint pIndex)
		{
			uint tIndex = (pIndex > 0)? (pIndex < count)? pIndex : count : 0;

			++count;
			T* buff = new T[count];
			for(uint i = 0; i != tIndex; ++i) buff[i] = value[i];
			buff[tIndex] = pValue;
			for(uint i = tIndex+1; i != count; ++i) buff[i] = value[i];
			
			delete value;
			value = buff;
		}
		
		public://Operators
		T operator[](uint pIndex) {return value[pIndex];}
	};
}
}